import hashlib
from flask import Flask, request, jsonify
from script.exe_sql import get_user_token

app = Flask(__name__)


@app.route("/bili", methods=["POST"])
def bili():
    """
    请求的数据格式要求:{"order_string":"......"}
    :return:
    """
    token = request.args.get("token")
    if not token:
        return jsonify({"status": False, "msg": "not found token"})

    user_token = get_user_token(token)
    if token not in user_token:
        return jsonify({"status": False, "msg": "用户token不存在"})
    print(request.json)
    order_string = request.json.get("order_string")
    if not order_string:
        return jsonify({"status": False, "msg": "参数错误"})
    encrypt_string = order_string + "351dafb1-1059-447f-9e86-5a06815a490d"
    obj = hashlib.md5(encrypt_string.encode("utf-8"))
    sign = obj.hexdigest()
    return jsonify({"status": True, "data": sign})


@app.route("/submit_geography", methods=["POST"])
def submit_geography():
    name = request.json.get("name")
    latitude = request.json.get("latitude")
    longitude = request.json.get("longitude")
    type = request.json.get("type")
    print(name)
    return jsonify({
        "status": True,
        "name": name,
        "latitude": latitude,
        "longitude": longitude,
        "type": type,
    })


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5001)
